<?php declare(strict_types=1);

namespace App\Rpc\Validator;

use App\Lib\Error\ErrorConstant;
use App\Rpc\Model\Dao\ModuleDao;
use Swoft\Bean\BeanFactory;
use Swoft\Validator\Annotation\Mapping\Validator;
use Swoft\Validator\Exception\ValidatorException;
use Swoft\Validator\Contract\ValidatorInterface;
use Swoft\Validator\Annotation\Mapping\IsString;
use Swoft\Validator\Annotation\Mapping\Length;
use Swoft\Validator\Annotation\Mapping\IsInt;
use Swoft\Db\DB;

/**
 * Class ModuleValidator
 *
 * @Validator("ModuleValidator")
 *
 * @package App\Rpc\Validator
 */
class ModuleValidator implements ValidatorInterface
{
    /**
     * @IsString(message="item_id类型不正确")
     * @var string
     */
    protected $item_id;

    /**
     * @IsString(message="模块名称不正确")
     *
     * @var string
     */
    protected $name;

    /**
     * @IsString(message="模块唯一标识格式不正确")
     * @Length(min=3, max=64, message="模块唯一标识格式不正确")
     * @var string
     */
    protected $module_value;

    /**
     * @IsInt(message="冻结状态类型不正确")
     *
     * @var int
     */
    protected $frozen_state;

    /**
     * @IsInt(message="创建时间不合法")
     *
     * @var int
     */
    protected $created_at;

    /**
     * @IsInt(message="更新时间不合法")
     *
     * @var int
     */
    protected $updated_at;

    /**
     * @param array $data
     * @param array $params
     * @return array
     * @throws ValidatorException
     */
    public function validate(array $data, array $params): array
    {
        $op = $params['op'] ?? null;
        $valid_item_id = $data['item_id'] ?? null;
        $name = $data['name'] ?? null;
        $module_value = $data['module_value'] ?? null;

        if (empty($valid_item_id) && $op == 'add') {
            throw new ValidatorException("唯一item_id必须填写", ErrorConstant::VALIDATE_ERROR_CODE);
        }

        if ($op == 'add' && strlen($valid_item_id) != 32) {
            throw new ValidatorException("唯一item_id格式不正确", ErrorConstant::VALIDATE_ERROR_CODE);
        }

        //验证是否存在
        if ($op == 'add' && !$name) {
            throw new ValidatorException("请输入套餐名称", ErrorConstant::VALIDATE_ERROR_CODE);
        }

        //验证是否存在
        if ($op == 'add' && !$module_value) {
            throw new ValidatorException("请输入模块唯一标识", ErrorConstant::VALIDATE_ERROR_CODE);
        }

        if ($name) {
            //验证账号是否唯一
            /** @var ModuleDao $ModuleDao */
            $ModuleDao = BeanFactory::getBean(ModuleDao::class);
            $first = $ModuleDao->getByWhere(['name' => $name]);
            if ($first) {
                throw new ValidatorException("模块名称已经添加", ErrorConstant::VALIDATE_ERROR_CODE);
            }
        }

        if($module_value){
            //验证账号是否唯一
            /** @var ModuleDao $ModuleDao */
            $ModuleDao = BeanFactory::getBean(ModuleDao::class);
            $first = $ModuleDao->getByWhere(['module_value' => $module_value]);
            if ($first) {
                throw new ValidatorException("模块唯一标识已经添加", ErrorConstant::VALIDATE_ERROR_CODE);
            }
        }

        return $data;
    }
}
